package com.baidu.web;

import com.baidu.common.bean.Goods;
import com.baidu.common.bean.Role;
import com.baidu.common.bean.User;
import com.baidu.common.exception.WebException;
import com.baidu.common.finalword.WebConstant;
import com.baidu.service.GoodsService;
import com.baidu.service.UserService;
import com.baidu.service.impl.GoodsServiceImpl;
import com.baidu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/19/20:06
 */

public class UserContorller extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private GoodsService goodsService = new GoodsServiceImpl();

    public UserContorller() throws WebException {
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestPath = req.getRequestURL().toString();

        if (requestPath.contains(WebConstant.USER_LOGOUT_STR)){
            req.getSession().invalidate();
            req.getRequestDispatcher("/login.jsp").forward(req,resp);
        }else if (requestPath.contains(WebConstant.USER_TO_REGISTER_STR)){
            req.getRequestDispatcher("/WEB-INF/page/register.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestPath = req.getRequestURL().toString();

        if (requestPath.contains(WebConstant.USER_LOGIN_STR)){
            User loginUser = (User)req.getSession().getAttribute("user");
            if (loginUser!=null){
                toList(req, resp);
                return;
            }

            String userName = req.getParameter("userName");
            String password = req.getParameter("password");
            String setCookie = req.getParameter("cookie");
            String sql = req.getParameter("sql");

            User user;
            try {
                user = userService.getByLogin(userName,password,sql);
            } catch (WebException e) {
                e.printStackTrace();
                req.setAttribute(WebConstant.GOODS_MESSAGE,"身份验证过程中出现异常，请稍后重试");
                req.getRequestDispatcher(WebConstant.USER_LOGIN_INFO_PAGE_PATH).forward(req,resp);
                return;
            }

            if (user.getAccount()==null || "".equals(user.getAccount())){
                req.setAttribute(WebConstant.GOODS_MESSAGE,"您的账号或密码错误");
                req.getRequestDispatcher(WebConstant.USER_LOGIN_INFO_PAGE_PATH).forward(req,resp);
                return;
            }


            req.getSession().setAttribute("isAdmin",getIsAdmin(user));

            req.getSession().setAttribute("user",user);

            if (WebConstant.USER_ON_STR.equals(setCookie)){
                Cookie cookie = new Cookie(user.getAccount(), user.getPassword());
                cookie.setMaxAge(60);
                resp.addCookie(cookie);
            }

            toList(req, resp);
        }else if (requestPath.contains(WebConstant.USER_REGISTER)){
            String userName = req.getParameter("userName");
            String account = req.getParameter("account");
            String password = req.getParameter("password");

            User user = new User();
            user.setUserName(userName);
            user.setAccount(account);
            user.setPassword(password);

            try {
                int i = userService.add(user);

                if (i==1){
                    req.setAttribute(WebConstant.GOODS_MESSAGE,"注册成功，即将返回登录页面...");
                    req.getRequestDispatcher(WebConstant.USER_LOGIN_INFO_PAGE_PATH).forward(req,resp);
                }
                req.setAttribute(WebConstant.GOODS_MESSAGE,"注册失败，请重新注册...");
                req.getRequestDispatcher(WebConstant.USER_LOGIN_INFO_PAGE_PATH).forward(req,resp);
            } catch (WebException e) {
                e.printStackTrace();
                req.setAttribute(WebConstant.GOODS_MESSAGE,e.getMessage());
                req.getRequestDispatcher(WebConstant.USER_LOGIN_INFO_PAGE_PATH).forward(req,resp);
            }
        }
    }

    private boolean getIsAdmin(User user) {
        List<Role> roles = user.getRoles();
        for (Role role : roles) {
            if ("admin".equals(role.getName())){
                return true;
            }
        }
        return false;
    }

    public void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            List<Goods> goodsList = goodsService.queryGoodsTypeList();
            req.setAttribute("goodsList",goodsList);
            req.getRequestDispatcher("/WEB-INF/page/goodsList.jsp").forward(req,resp);
        } catch (WebException e) {
            e.printStackTrace();
        }
    }
}
